
#{extends 'main.html' /}
#{set title:'首页设置' /}
<script type="text/javascript">

    var imagefile = '${imagefile}';
    var product_index = -1 ;
    $(function () {
        if(imagefile != null && imagefile != '' && imagefile != 'undefined'){
            initImg(imagefile);
            $("#addli").hide();
        }


        $(document).on('click','.open-select', function () {
            product_index = $(this).attr('product_index');
            $.popup('.popup-select1');
        });
        $(document).on('click','.popup-overlay', function () {
            $.closeModal();
        });

        $('.popup-select1 dl.one dt').click(function(){
            var firstTypeId = $(this).attr('first_type_id');
            $("#secondTypeList").empty();
            $.getJSON("/product/secondLevel", {id: firstTypeId},
                    function (data) {
                        console.log(data,data)
                        if(data.list.length > 0){
                            $.each(data.list,function(index,value){
                                $("#secondTypeList").append("<li onclick='showProduct("+value.id+")'  second_type_id='"+value.id+"'>"+value.name+"</li>");
                            })
                        }else{

                        }

                    })
            $('.popup-select1 dl.one dt').removeClass('curr');
            $(this).addClass('curr');
            $('.popup-select1 ul.three').removeClass('curr');
            $('.popup-select1 ul.two').removeClass('curr1');
            $('.popup-select1 ul.two').addClass('curr');
        });


        $.init();

    })

    function showProduct(value){
        $("#productList").empty();
        $.getJSON("/product/getProductByType", {typeId: value},
                function (data) {
                    console.log(data,data)
                    if(data.productList.length > 0){
                        //'"+value.name+"'
                        $.each(data.productList,function(index,value){
                            $("#productList").append("<li  onclick=onSetProduct("+value.id+",'"+value.name+"',"+value.merchantPrice+")  checked_product_id='"+value.id+"'>"+value.name+"</li>");

                        })
                    }else{
                    }
                })
        $('.popup-select1 ul.two li').removeClass('curr');
        $(this).addClass('curr');
        $('.popup-select1 ul.two').removeClass('curr');
        $('.popup-select1 ul.two').removeClass('curr1');
        $('.popup-select1 ul.two').addClass('curr1');
        $('.popup-select1 ul.three').addClass('curr');
    }

    function onSetProduct(id,name,merchantPrice){
        $('#product'+product_index+'_product_id').val(id);
        $('#product'+product_index+'_product_name').val(name);
        $('#product'+product_index+'_price').val(merchantPrice);
        $('.popup-select1 dl.one dt').removeClass('curr');
        $('.popup-select1 ul.three').removeClass('curr');
        $('.popup-select1 ul.two').removeClass('curr1');
        $.closeModal();
    }




</script>
<script type="text/javascript" src="/public/sui/js/lrz.all.bundle.js"></script>
<div class="page-group">
    <div class="page">
        <header class="bar bar-nav">
            <a href="javascript:history.go(-1);" class="icon icon-left pull-left"></a>
            <h1 class="title">添加秒杀产品</h1>
        </header>
        <section class="product_add content scroll_y">
            <div class="list-block">
                <form id="idForm" method="post" action="/index/seckill/save">
                    <input type="hidden" name="seckillActivity.id" value="${seckillActivity?.id}">

                    <ul>
                        <!-- Text inputs -->
                        <li>
                            <div class="item-content">
                                <div class="item-inner">
                                    <div class="item-title label">开始时间</div>
                                    <div class="item-input">
                                        <input type="text" name="seckillActivity.beginAtStr" id='beginTime' value="${seckillActivity?.beginAt}">
                                    </div>
                                </div>
                            </div>
                        </li> <li>
                            <div class="item-content">
                                <div class="item-inner">
                                    <div class="item-title label">结束时间</div>
                                    <div class="item-input">
                                        <input type="text" name="seckillActivity.endAtStr" id='endTime' value="${seckillActivity?.endAt}">
                                    </div>
                                </div>
                            </div>
                        </li> <li>
                            <div class="item-content">
                                <div product_index="0" class="item-inner open-select">
                                    <div class="item-title label">秒杀一</div>
                                    <div class="item-input">
                                        <input type="hidden" name="product0.id" value="${seckillActivity?.activityProducts?.size() > 0 ? seckillActivity?.activityProducts?.get(0)?.id : null}">
                                        <input type="hidden" id="product0_product_id" name="product0.product.id" value="${seckillActivity?.activityProducts?.size() > 0 ? seckillActivity?.activityProducts?.get(0)?.product?.id:null}">
                                        <input type="text" readonly id="product0_product_name" value="${seckillActivity?.activityProducts?.size() > 0 ? seckillActivity?.activityProducts?.get(0)?.product?.name: ''}${seckillActivity?.activityProducts?.size() > 0 ? seckillActivity?.activityProducts?.get(0)?.product?.spec: ''}">

                                    </div>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content">
                                <div class="item-inner">
                                    <div class="item-title label">设置价格</div>
                                    <div class="item-input">
                                        <input id="product0_price" type="number" name="product0.activityPrice" value="${seckillActivity?.activityProducts?.size() > 0 ? seckillActivity?.activityProducts?.get(0)?.activityPrice : null}" placeholder="0.00">
                                    </div>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content">
                                <div product_index="1" class="item-inner open-select">
                                    <div class="item-title label">秒杀二</div>
                                    <div class="item-input">
                                        <input type="hidden" name="product1.id" value="${seckillActivity?.activityProducts?.size() > 1 ? seckillActivity?.activityProducts?.get(1)?.id : null}">
                                        <input type="hidden" id="product1_product_id" name="product1.product.id" value="${seckillActivity?.activityProducts?.size() > 1 ? seckillActivity?.activityProducts?.get(1)?.product?.id : null}">
                                        <input type="text" readonly id="product1_product_name" value="${seckillActivity?.activityProducts?.size() > 1 ? seckillActivity?.activityProducts?.get(1)?.product?.name:''}${seckillActivity?.activityProducts?.size() > 1 ? seckillActivity?.activityProducts?.get(1)?.product?.spec:''}">

                                    </div>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content">
                                <div class="item-inner">
                                    <div class="item-title label">设置价格</div>
                                    <div class="item-input">
                                        <input id="product1_price" type="number" name="product1.activityPrice" value="${seckillActivity?.activityProducts?.size() > 1 ? seckillActivity?.activityProducts?.get(1)?.activityPrice:null}" placeholder="0.00">
                                    </div>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content">
                                <div product_index="2" class="item-inner open-select">
                                    <div class="item-title label">秒杀三</div>
                                    <div class="item-input">
                                        <input type="hidden" name="product2.id" value="${seckillActivity?.activityProducts?.size() > 2 ?seckillActivity?.activityProducts?.get(2)?.id : null}">
                                        <input type="hidden" id="product2_product_id" name="product2.product.id" value="${seckillActivity?.activityProducts?.size() > 2 ? seckillActivity?.activityProducts?.get(2)?.product?.id : null}">
                                        <input type="text" readonly id="product2_product_name" value="${seckillActivity?.activityProducts?.size() > 2 ? seckillActivity?.activityProducts?.get(2)?.product?.name :''}${seckillActivity?.activityProducts?.size() > 2 ? seckillActivity?.activityProducts?.get(2)?.product?.spec:''}">

                                    </div>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="item-content">
                                <div class="item-inner">
                                    <div class="item-title label">设置价格</div>
                                    <div class="item-input">
                                        <input id="product2_price" type="number" name="product2.activityPrice" value="${seckillActivity?.activityProducts?.size() > 2 ? seckillActivity?.activityProducts?.get(2)?.activityPrice : null}" placeholder="0.00">
                                    </div>
                                </div>
                            </div>
                        </li>

                    </ul>
                    <div class="content-block">
                        <input type="button" name="button" class="button button-fill button-pink" id="button"
                               value="保存" onclick="saveProducts()">
                        <p>注：请先上传产品,每次需设置三款产品</p>
                    </div>

                </form>
            </div>

        </section>

    #{include 'footer.html'/}
    </div>
    <script>
        $.init();
    </script>
</div>
<!-- About Popup -->
<div class="popup popup-select1" style="top:30%; height:70%;">
    <div class="popup_con">
        <dl class="one scroll_y">
        #{if firstTypeList != null && firstTypeList.size() > 0 }
            #{list firstTypeList , as:'firstType'}
                <dt first_type_id="${firstType?.id}" >${firstType?.name}</dt>
            #{/list}
        #{/if}
        </dl>
        <ul class="two scroll_y" id="secondTypeList">



        </ul>
        <ul class="three scroll_y" id="productList">


        </ul>

    </div>
</div>

<script type="text/javascript">
    var beginAt = '${beginAt}';
    var endAt = '${endAt}';
    var product_index = -1 ;
    $(function () {


        $.init();


        initActivityTime('beginTime',new Date(beginAt));
        initActivityTime('endTime',new Date(endAt));

    });
    //初始化时间,
    // 从后台传递过来的时间格式是js可以识别的格式,
    // 可以获取到时间的年,月,日等参数
    function initActivityTime(selector , dateTime) {

        var year = dateTime.getFullYear();
        var month = dateTime.getMonth() + 1;
        var date = dateTime.getDate();
        var hours = dateTime.getHours();
        var minutes = dateTime.getMinutes();
//        alert('nowTime:'+nowTime+'' +
//                '\n year:'+year +
//                '\n month:' +month +
//                '\n date:'+date +
//                '\n hours :'+hours +
//                '\n minutes :'+minutes +
//                '\n'+isNaN(date) +
//                '\n'+(date  > 10));
        month = month < 10 ?  '0' + month : month ;
        date = date < 10 ?  "0" + date :date ;
        hours = hours < 10 ?"0" + hours :hours ;
        minutes = minutes < 10? "0" + minutes : minutes;

        $('#'+selector).datetimePicker({
            value: [year, month, date, hours, minutes]
        });
    }

    function getNowTime(dateTime) {
        var year = dateTime.getFullYear();
        var month = dateTime.getMonth() + 1;
        var date = dateTime.getDate();
        var hours = dateTime.getHours();
        var minutes = dateTime.getMinutes();
        var nowTime = year + '-' + (month <10 ? '0'+month : month)+ '-' + (date <10 ? '0'+date : date) + ' '+ (hours <10 ? '0' + hours : hours) +':'+ (minutes <10 ? '0' + minutes : minutes);
        return nowTime
    }

    //保存
    function saveProducts() {
        var beginAt = $('#beginTime').val();
        var endAt = $('#endTime').val();
        var nowTime = getNowTime(new Date());

        var product0 = $('#product0_product_id').val();
        var product1 = $('#product1_product_id').val();
        var product2 = $('#product2_product_id').val();
        if(beginAt == null || beginAt == ''){
            $.alert('请填写活动开始时间')
            return ;
        }
        if(endAt == null || endAt == ''){
            $.alert('请填写活动结束时间');
            return ;
        }

        if( beginAt <= nowTime){
            $.alert('活动开始时间不能小于当前时间!')
            return ;
        }

        if( beginAt >= endAt){
            $.alert('活动开始时间不能大于结束时间');
            return ;
        }


        if(isBank(product0) || isBank(product1) || isBank(product2) ){
            $.alert('每次需设置三款不同产品')
            return ;
        }
        if(product0 == product1 || product0 == product2 || product1 == product2){
            $.alert('三款产品不能重复');
            return ;
        }

        $('#idForm').submit();

    }
    function isBank(product) {
        if(product == null || product == '' || product == 'undefined')
            return true;
        return  false ;

    }
</script>
